package cn.controller;

import cn.basic.BaseController;
import cn.dao.ServiceDAO;
import cn.factory.DAOFactory;
import cn.model.TService;
import cn.model.VServiceEquipment;
import cn.model.VUserRole;
import cn.util.Expression;
import cn.util.Toolsutil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 维护信息数据交互
 * @author WZG
 * @version 2020-2-30
 */
@Controller
@RequestMapping(value="/service")
public class ServiceController extends BaseController {

    @RequestMapping(value="/addservice")
    public void AddService(
            Integer equipid,
            String reasons,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException {
        VUserRole user = (VUserRole)session.getAttribute("loginuser");
        TService service =new TService();
        service.setEquipid(equipid);
        service.setUserid(user.getUserid());
        service.setServiceTime(Toolsutil.getdatetimes());
        service.setAudit(1);
        service.setReasons(reasons);
        ServiceDAO serdao = DAOFactory.getServiceDAO();
        if (serdao.addService(service) > 0) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "添加成功";
        } else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "添加失败";
        }
        closeout();
    }

    /*
     * 实现一个的删除
     * @param
     * @throws IOException
     */
    @RequestMapping(value="/deleteservice")
    public void deleteService(
            String serviceid,
            HttpServletRequest request,
            HttpServletResponse response,Model model) throws IOException{
        ServiceDAO serdao = DAOFactory.getServiceDAO();
        if (serviceid !=null && !serviceid.equals("")){
            serdao.deleteService(Integer.parseInt(serviceid));
            dt.code = dt.FLAG_SUCC;
            dt.msg = "删除成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "删除失败 ";
        }
        closeout();
    }

    @RequestMapping(value="/modifynotice")
    public void modifyNotice(
            Integer serviceid,
            Integer equipid,
            String reasons,
            Integer audit,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{
        VUserRole user = (VUserRole)session.getAttribute("loginuser");
        ServiceDAO serdao = DAOFactory.getServiceDAO();
        TService service =new TService();
        service.setServiceid(serviceid);
        service.setEquipid(equipid);
        service.setUserid(user.getUserid());
        service.setAudit(audit);
        service.setReasons(reasons);

        if (serdao.modifyService(service)) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "修改成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "修改失败";
        }
        closeout();
    }


    @RequestMapping(value="/modifyaudit")
    public void modifyNoticeById(
            Integer serviceid,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        ServiceDAO serdao = DAOFactory.getServiceDAO();

        if (serdao.modifyServiceByid(serviceid,2)) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "修改成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "修改失败";
        }
        closeout();
    }



    @RequestMapping(value="/getservicelistByPage")//映射路径,拦截post请求
    public void getServiceList(
            Integer equipid,
            String userid,
            String serviceTime,
            Integer page,
            Integer limit,//一页多少条

            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException {
        ServiceDAO serdao = DAOFactory.getServiceDAO();
        Expression exp = new Expression();
        if (equipid != null && !equipid.equals("")) {
            exp.andEqu("equipid", equipid, String.class);
        }
        if (userid != null && !userid.equals("")) {
            exp.andEqu("userid", userid, String.class);
        }
        if (serviceTime != null && !serviceTime.equals("")) {
            exp.andEqu("serviceTime", serviceTime, String.class);
        }
        String wherecondition = exp.toString();

        List<VServiceEquipment> list = serdao.getServiceList(wherecondition, page, limit);
        int size = serdao.getServiceAmount(wherecondition);
        if (list != null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + dt.count;
            dt.data = list;
            dt.count = size;
        } else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }

    @RequestMapping(value="/getservicelistByAudit")//映射路径,拦截post请求
    public void getServiceListByAudit(
            String equipname,
            String type,
            String rolename,
            Integer page,
            Integer limit,//一页多少条

            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException {
        ServiceDAO serdao = DAOFactory.getServiceDAO();
        Expression exp = new Expression();
        if (equipname != null && !equipname.equals("")) {
            exp.andEqu("equipname", equipname, String.class);
        }
        if (type != null && !type.equals("")) {
            exp.andEqu("type", type, String.class);
        }
        if (rolename !=null && !rolename.equals("")){
            exp.andEqu("rolename",rolename,String.class);
        }
        String wherecondition = exp.toString();

        List<VServiceEquipment> list = serdao.getServiceStatus(1,page, limit);
        int size = serdao.getServiceAmount(wherecondition);
        if (list != null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + dt.count;
            dt.data = list;
            dt.count = size;
        } else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }
    @RequestMapping(value="/getserviceByuser")//映射路径,拦截post请求
    public void getServiceListByUser(
            Integer page,
            Integer limit,//一页多少条

            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException {
        ServiceDAO serdao = DAOFactory.getServiceDAO();

        VUserRole user = (VUserRole)session.getAttribute("loginuser");
        String userid = user.getUserid();
        List<VServiceEquipment> list = serdao.getServiceByUser(userid,page, limit);
        int size = serdao.getServiceAmount(userid);
        if (list != null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + dt.count;
            dt.data = list;
            dt.count = size;
        } else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }
}
